<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>

<%@include file="head.jsp"%>

<%
	String id = request.getParameter("id");
	id = id == null ? "create" : id;
%>


<div class="am-cf admin-main">
	<!-- content start -->
	<div class="admin-content">
		<div class="admin-content-body">
			<div class="am-cf am-padding am-padding-bottom-0">
				<div class="am-fl am-cf">
					<strong class="am-text-primary am-text-lg">构建项目</strong> / <small>项目基础信息</small>
				</div>
			</div>

			<hr>
			<div class="am-g">
				
							<div class="am-form-group am-fr">
								<button type="button" id="id_project_back"
									class="am-btn am-btn-primary">上一步</button>
								<button type="button" id="id_project_save"
									class="am-btn am-btn-primary">保存修改</button>
								<button type="button" id="id_project_next"
									class="am-btn am-btn-primary">下一步</button>
							</div>
			</div>
			<div class="am-g">
				<div class="am-u-sm-12 am-u-md-4 am-u-md-push-8"></div>

				<div class="am-u-sm-12 am-u-md-8 am-u-md-pull-4">
					<form class="am-form am-form-horizontal" id="id_from"
						action="/save" method="post" data-am-validator>
						<fieldset>
							<input type="hidden" id="id_id" value="<%=id%>">
							<div class="am-form-group">
								<label for="dbName" class="am-u-sm-3 am-form-label">GroupID</label>
								<div class="am-u-sm-9">
									<input type="text" id="groupId" required name="groupId"
										value="" placeholder="com.easymvc">
								</div>
							</div>

							<div class="am-form-group">
								<label for="driver" class="am-u-sm-3 am-form-label">Artifact
									id</label>
								<div class="am-u-sm-9">
									<input type="text" id="artifactId" required name="artifactId"
										value="" placeholder="com.easymvc">

								</div>
							</div>


							<div class="am-form-group">
								<label for="url" class="am-u-sm-3 am-form-label">Version</label>
								<div class="am-u-sm-9">
									<input type="text" id="versionId" required name="versionId"
										value="" placeholder="1.0.0">
								</div>
							</div>


							<div class="am-form-group">
								<label for="username" class="am-u-sm-3 am-form-label">jdbc驱动名称</label>
								<div class="am-u-sm-9">
									<input type="text" id="driverClassName" required
										name="driverClassName" value="" placeholder="com.jdbc.driver">
									<small></small>
								</div>
							</div>

							<div class="am-form-group">
								<label for="username" class="am-u-sm-3 am-form-label">输出路径</label>
								<div class="am-u-sm-9">
									<input type="text" id="basePath" required name="basePath"
										value="" placeholder=""> <small></small>
								</div>
							</div>

							<div class="am-form-group">
								<label for="password" class="am-u-sm-3 am-form-label">项目前缀</label>
								<div class="am-u-sm-9">
									<input type="text" id="pix" required name="pix" value=""
										placeholder="easymvc"> <small></small>
								</div>
							</div>

							<div class="am-form-group">
								<label for="schema" class="am-u-sm-3 am-form-label">扫描包名</label>
								<div class="am-u-sm-9">
									<input type="text" id="scanPackage" required name="scanPackage"
										value="" placeholder="com.easymvc"> <small></small>
								</div>
							</div>

							<div class="am-form-group">
								<label for="schema" class="am-u-sm-3 am-form-label">MAPPER包名</label>
								<div class="am-u-sm-9">
									<input type="text" id="mapperPackage" required
										name="mapperPackage" value="" placeholder="com.easymvc">
									<small></small>
								</div>
							</div>

							<div class="am-form-group">
								<label for="schema" class="am-u-sm-3 am-form-label">PO包名</label>
								<div class="am-u-sm-9">
									<input type="text" id="poPackage" required name="poPackage"
										value="" placeholder="com.easymvc"> <small></small>
								</div>
							</div>

							<div class="am-form-group">
								<label for="schema" class="am-u-sm-3 am-form-label">控制层包名</label>
								<div class="am-u-sm-9">
									<input type="text" id="controllerPackage" required
										name="controllerPackage" value="" placeholder="com.easymvc">
									<small></small>
								</div>
							</div>
							<!-- <div class="am-form-group">
								<label for="schema" class="am-u-sm-3 am-form-label">数据库(主库)</label>
								<div class="am-u-sm-9">
									<select data-am-selected="{dropUp: 1}" id="masterDb">
									</select>
									<button type="button" id="new_db_slaver"
										class="am-btn am-btn-primary js-modal-open">新增数据库连接</button>
								</div>
							</div>
							<div class="am-form-group">
								<label for="schema" class="am-u-sm-3 am-form-label">数据库(丛库)</label>
								<div class="am-u-sm-9">
									<select data-am-selected="{dropUp: 1}" id="slaverDb">
									</select>
								</div>
							</div> -->
							<div class="am-form-group">
								<label for="schema" class="am-u-sm-3 am-form-label">项目名称</label>
								<div class="am-u-sm-9">
									<input type="text" id="menuName" required
										name="menuName" value="" placeholder="com.easymvc">
									<small></small>
								</div>
							</div>
						</fieldset>

					</form>
				</div>
			</div>
		</div>

	
	</div>




	<footer class="admin-content-footer">
		<hr>
		<p class="am-padding-horizontal">© easymvc &nbsp;&nbsp;&nbsp;</p>
	</footer>

</div>



<script type="text/javascript">
	var projectId = $("#id_id").val();
	var metadata = [ 'id', 'groupId', 'artifactId', 'versionId',
			'driverClassName', 'basePath', 'pix', 'scanPackage',
			'mapperPackage', 'poPackage', 'controllerPackage', 'masterDb',
			'slaverDb','menuName' ];

	$(function() {
		loadProject();

		$("#id_project_save").on("click", function() {
			saveProject();
		});

		$("#id_project_next").on("click", function() {
			nextStep();
		});
		
		$("#id_project_back").on("click", function() {
			window.history.go(-1);
		});
	});


	function loadProject() {
			$.post("mvc/project/select?id=" + projectId, function(data) {
				for ( var _idx in metadata) {
					var idkey = metadata[_idx];
					if (idkey != 'id') {
						$("#" + idkey).val(data[idkey]);
					}
				}
			});
	}
	function saveProject(c) {
		c = c || function() {
		};
		var isVal = $('#id_from').validator('isFormValid');

		if (isVal) {
			var _data = {};
			for ( var _idx in metadata) {
				var idkey = metadata[_idx];
				var value = $("#" + idkey).val();
				if (value == 0 || value) {
					//alert("idkey:" + idkey + ",val:" + value);
					_data[idkey] = value;
				}
				
			}

			var addorupdate = "add";
			if ($("#id_id").val() != "create") {
				addorupdate = "update";
				_data["id"] = projectId;
			}
			
			/*for ( var _idx in _data) {
				alert("_idx:" + _idx + ",val:" +_data[_idx]);
			}*/
			/*$.post("mvc/project/" + addorupdate, _data, function(data) {
				if (data && data.success) {
					projectId = data.data;
					$("#id_id").val(projectId);
					//$('#id_from')[0].reset();
					c();
				} else {
					alert(data.message);
				}
			});*/
			
			/*var data1 = {};
			data1.name = "test";
			data1.password = "123456";*/
	
			
			$.ajax({
				type: 'get',
				url: "mvc/project/" + addorupdate,
				data: _data,
				contentType: "application/json;charset=utf-8;",
				success: function(data) {
					if (data && data.success) {
						projectId = data.data;
						$("#id_id").val(projectId);
						//$('#id_from')[0].reset();
						c();
					} else {
						alert(data.message);
					}
				}
			});
			
		}
	}
	function resetSelect(data, id) {
		var selectObj = $("#" + id);
		selectObj.empty();
		for ( var _idx in data) {
			var _it = data[_idx];
			var tmp = '<option value="' + _it["id"] + '" selected>'
					+ _it['jdbcUrl'] + '</option>';
			selectObj.append(tmp);
		}
	}
	function closeDBDiv() {
		$("#new-db").modal("close");
	}
	function nextStep() {
		saveProject(function() {
			window.location.href = "prjbiz.jsp?id=" + projectId;
		});
	}
</script>
<%@include file="bottom.jsp"%>




